<script setup lang='ts'>
import { nextTick, reactive, ref } from 'vue';
import {numberApi, statisticsApi} from '@/api/controller'
import { ToastSuccess, Toastloading } from '@/utils/public';
const formLabelWidth = '120px'
const dataForm = ref()
const $emit = defineEmits(['updateList'])
const action = reactive({
  dialogFormVisible:false,
  title:''
})
const form = reactive({
  user_uuid:'',
  invite_code:'',
  invite_nickname:'',
})
const formRules = {

}
const handleSubmit = ()=>{
  dataForm.value.validate(async(valid:any)=>{
    if(valid) {
      const loading = Toastloading()
      await statisticsApi.changeBindInviteCode(form)
      ToastSuccess('修改成功')
      loading.close()
      closeDialog()
      $emit('updateList')
    }
  })
}
const openDialog = (query:any = {})=> {
  const { user_uuid='', invite_nickname='', invite_code='' } = query;
  Object.assign(form,{
    user_uuid,
    invite_code:invite_code,
  })
  action.title = `${invite_nickname}绑定邀请码`
  action.dialogFormVisible = true
}
const closeDialog = ()=>{
  Object.assign(form,{
    user_uuid:'',
    invite_code:'',
    invite_nickname:'',
  })
  nextTick(() => {
    dataForm.value.clearValidate();
  });
  action.dialogFormVisible = false
}
defineExpose({
  openDialog
})
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="false"
      :show-close="false"
      :close-on-press-escape="false"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules" :label-width="formLabelWidth">
        <el-form-item label="邀请人邀请码" prop="invite_code" >
          <el-input v-model="form.invite_code" />
        </el-form-item>
        <el-form-item label="被邀请人UUID" prop="user_uuid" >
          <el-input v-model="form.user_uuid" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped >
.xm-dialog{
  .el-input {
    width: calc($inputWidth);
  }
}
</style>
